package com.zlzxm.shoesmarket.view.fragment;

import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.globle.Constant;
import com.zlzxm.shoesmarket.util.KeyboardUtil;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.window.wheelpick.EntityOption;
import com.zlzxm.shoesmarket.view.window.wheelpick.OnOptionSelectListener;
import com.zlzxm.shoesmarket.view.window.wheelpick.WindowWheelPick;
import com.zlzxm.shoesmarket.viewmodel.VMAddNote;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * CREATE BY zlzxm ON 2021/5/17
 */
public class FMAddNote extends BaseFragment implements View.OnClickListener{

    private static String Bundlekey = "type";
    public static String type_out = "out";
    public static String type_in = "in";


    public static FMAddNote newInstance(String type) {

        Bundle args = new Bundle();
        args.putString(Bundlekey,type);
        FMAddNote fragment = new FMAddNote();
        fragment.setArguments(args);
        return fragment;
    }

    private FMAddNote( ) {
    }


    VMAddNote vmAddNote;

    private String type;

    EditText etInput ;

    KeyboardUtil keyboardUtil;

    KeyboardView keyboardView;

    RelativeLayout rlRoot;

    TextView tvType;


    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getArguments().getString(Bundlekey);


    }

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {


        return inflater.inflate(R.layout.fm_add_note,container,false);
    }

    @Override
    protected void initView(@NonNull @NotNull View view, @NonNull @NotNull Bundle savedInstanceState) {
        super.initView(view, savedInstanceState);

        etInput = (EditText) ZViewHelp.findById(view,R.id.etInput);
        etInput.setInputType(InputType.TYPE_NULL);
        ZViewHelp.disableShowSoftKeyBoard(etInput);
        keyboardView = ZViewHelp.findById(view,R.id.key);
        tvType = ZViewHelp.findById(view,R.id.tvType);
        rlRoot = ZViewHelp.setOnClickListener(view,R.id.rlRoot,this);
        ZViewHelp.setOnClickListener(view,R.id.tvSave,this);
        ZViewHelp.setOnClickListener(view,R.id.rlType,this);

        etInput.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                keyboardUtil =  new KeyboardUtil(getContext(),keyboardView,etInput);
                keyboardUtil.showKeyboard();
                return false;
            }
        });

    }

    @Override
    protected void initViewModel() {
        super.initViewModel();
        vmAddNote = new ViewModelProvider(this).get(VMAddNote.class);
        vmAddNote.liveType.setValue(type.equals(type_out)?"2":"1");
        vmAddNote.liveSelectType.observe(this,tvType::setText);

        vmAddNote.getLiveErrorToast().observe(this,this::errorToast);
        vmAddNote.getLiveSucceedToast().observe(this,this::succeedToast);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.rlRoot){

            if(keyboardUtil!=null)
                keyboardUtil.hideKeyboard();

        }else if (v.getId() == R.id.rlType){

            showWindow();

        }else if(v.getId() == R.id.tvSave){

            save();
        }


    }

    private void save() {

        vmAddNote.add(etInput.getText().toString());


    }

    private void showWindow() {

        List<EntityOption> list = new ArrayList<>();

        if (type.equals(type_out)){

            for (Constant.NoteOutType value : Constant.NoteOutType.values()) {


                list.add(new EntityOption("1",value.value));

            }


        }else if(type.equals(type_in)){

            for (Constant.NoteInType value : Constant.NoteInType.values()) {


                list.add(new EntityOption("1",value.value));

            }

        }

        WindowWheelPick.getInstance()
                .setOptions(list)
                .setOnOptionSelectListener(new OnOptionSelectListener() {
                    @Override
                    public void onSelected(EntityOption entityOption) {

                        vmAddNote.liveSelectType.setValue(entityOption.getName());
                    }
                })
                .show(getChildFragmentManager(),"");


    }
}
